public with sharing class MC_FilterController
{
    public ApexPages.StandardSetController standardController
    {
        get
        {
            if(standardController==null)
            {
                Database.QueryLocator query=GetFilterQuery();
                if(query==null)
                    return null;
                standardController=new ApexPages.StandardSetController(query);
                standardController.setPageSize(100);
            }
            return standardController;
        }
        set;
    }
    
    public String selectedGroupType {get; set; }
    
    public String selectedMeetingDay {get; set; }
    
    public void ResetMeetingDay()
    {
        selectedMeetingDay=null;
        ResetList();
    }
    
    public void ResetList()
    {
        standardController=null;
    }
    
    private Database.QueryLocator GetFilterQuery()
    {
        if(selectedGroupType==null)
            return null;
        if(selectedMeetingDay==null)
            return null;
            
        return Database.getQueryLocator(
            [select
                id
                ,group_name__c
                ,leader_name__c
                ,college_group_area__c
             from
                Missional_Community__c
             where
                college_group_type__c=:EncodingUtil.urlDecode(selectedGroupType, 'UTF-8')
                and meeting_day__c=:selectedMeetingDay
           ]);
    }
    
    public List<SelectOption> GetGroupTypes()
    {
        Set<String> groupTypes=new Set<String>();    //Use a set so that duplicates are eliminated
        for(Missional_Community__c community : [select college_group_type__c from Missional_Community__c order by college_group_type__c])
        {
            groupTypes.add(community.college_group_type__c);
        }
        
        List<SelectOption> options=new List<SelectOption>();
        options.add(new SelectOption('', 'Choose a group type...'));
        for(String groupType : groupTypes)
        {
        	if(groupType!=null)
        	{
            	SelectOption option=new SelectOption(EncodingUtil.urlEncode(groupType, 'UTF-8'), groupType);
            	options.add(option);
        	}
        }
        
        return options;
    }
    
    public List<SelectOption> GetMeetingDays()
    {
        Set<String> meetingDays=new Set<String>();
        if(selectedGroupType!=null)
        {
            for(Missional_Community__c community : [select meeting_day__c from Missional_Community__c where college_group_type__c=:EncodingUtil.urlDecode(selectedGroupType, 'UTF-8') order by meeting_day__c])
            {
                meetingDays.add(community.meeting_day__c);
            }
        }
        
        List<SelectOption> options=new List<SelectOption>();
        options.add(new SelectOption('', 'Choose a meeting day...'));
        for(String meetingDay : meetingDays)
        {
        	if(meetingDay!=null)
        	{
            	SelectOption option=new SelectOption(meetingDay, meetingDay);
            	options.add(option);
        	}
        }
        
        return options;
    }

    public List<Missional_Community__c> getMissional_communities()
    {
        if(standardController==null)
            return null;   
        return (List<Missional_Community__c>) standardController.getRecords();
    }

	public static testMethod void test1()
	{
		Missional_Community__c aGroup=new Missional_Community__c();
		aGroup.College_Group_Type__c='Freshman';
		aGroup.Meeting_Day__c='Monday';
		insert aGroup;
		
		MC_FilterController controller=new MC_FilterController();
		System.assertEquals(null, controller.getMissional_communities());
		
		controller.ResetMeetingDay();
		
		controller.selectedGroupType=controller.GetGroupTypes()[0].getValue();
		System.assertNotEquals(null, controller.selectedGroupType);
		
		controller.selectedMeetingDay=controller.GetMeetingDays()[0].getValue();
		System.assertNotEquals(null, controller.selectedMeetingDay);
		
		System.assertNotEquals(null, controller.getMissional_communities());
		
		controller.ResetMeetingDay();
		
		System.assertEquals(null, controller.getMissional_communities());
	}
}